// xc_lshop/shop/integral/index/index.js
var app = getApp()
var pro_classbill = require('../model/integralpro_class')
var actbill = require('../model/integralpro_contents')

Page({
  mixins: [require('../../../xc/mixins/basicMixin')],
  /**
   * 页面的初始数据
   */
  data: {
    basicurl: app.globalData.basicurl,
    actpid:"0",
    actspid:0,
    CustomBar: app.globalData.CustomBar,
 
  },
  buyop: function (ev) {
    if (!this.data.member.memberinfo) {
      this.login();
      return;
    }

    var dataset = ev.currentTarget.dataset
   
    var xcmodel=dataset.item;
    if(xcmodel.stock<=0){ 
      wx.showToast({ 
        icon:"none", 
        title: '产品已兑完', 
      }) 
      return; 
    }  
    // if(xcmodel.guigetype==-1){
    //   var newmodel={};
    //   newmodel['activityid']=0;
    //   newmodel['activityprice']=-1;
    //   newmodel['attrnameinfo']=[];
    //   newmodel['attrs']=[];
    //   newmodel['bpid']=-1;
    //   newmodel['cid']=0;
    //   newmodel['flag']=-1;
    //   newmodel['id']=xcmodel['id'];
    //   newmodel['integral']=xcmodel['integral'];
    //   newmodel['keyval']=xcmodel['id']+"_0_0_-1";
    //   newmodel['name']=xcmodel['name'];
    //   newmodel['num']=1;
    //   newmodel['pnum']=-2;
    //   newmodel['price']=xcmodel['price'];
    //   newmodel['pro_ident']=-1;
    //   newmodel['realprice']=xcmodel['realprice'];
    //   newmodel['simg']=xcmodel['simg'];
    //   newmodel['type']=xcmodel['type'];
    //   newmodel['weight']=xcmodel['weight'];
    //   console.log("ppppp")
    //   console.log(newmodel)

    //   app.globalData.temcart = {};
    //   app.globalData.temcart["data"] = [newmodel]; 
    //   wx.navigateTo({
    //     url: '/' + app.globalData.xcsys.m + '/integralmall/paycore/payinfo?iscart=-1'
    //   })
    //   return;
    // }
    var that = this
    var option = "";
 
    actbill.showattrchose(that, xcmodel.id, "", 3, "")
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  onclicknav(e){
    var that = this; 
    this.setData({
      actpid:e.currentTarget.dataset.id
    })
    this.getpagelist(that, 1)
  },
  ontabChange(e){
    var that = this; 
    this.setData({
      actpid:e.detail.name
    })
    this.getpagelist(that, 1)
  },
  getclass() {
    var that = this;
    //加载全部分类
    pro_classbill.apiall().then(item => {
      that.setData({
        navclass: item.obj.modellist,
        actpidx: 0
      })
    })
  },
  getpagelist(that, rest, strwhere) {
    if (rest == 1) {
      if (!strwhere) {
        strwhere = {};
      }
      strwhere["status"] = 1;
      strwhere["bpid"] = this.data.actpid;
      strwhere["spid"] = this.data.actspid;
      // strwhere["seachval"] = this.data.seachval;
      that.setData({ modellist: [] });
    }
    actbill.offset(that, rest, strwhere).then(function (result) {
      var updatemdoel = {};

      if ((result["code"] == "over" && result["obj"]) || result["code"] == "loadmore") {

         for(var a in result["obj"]["modellist"] ){
           if(result["obj"]["modellist"][a]){
            result["obj"]["modellist"][a]["snapshot"] = JSON.parse(result["obj"]["modellist"][a]["snapshot"])
           }
         }     
        if (rest == 1) {
          updatemdoel["modellist"] = result["obj"]["modellist"];
        }
        else {
          if (result["obj"]["modellist"].length > 0) {
            updatemdoel["modellist"] = that.data.modellist.concat(result["obj"]["modellist"]);
          }
        }
      }
      wx.stopPullDownRefresh();
      that.setData(updatemdoel);
    });
  },
  xcloaddata: function (options, config) {

    var that=this;
    var update = {};
    if (config.integral) {
      update['integral'] = config.integral
    }

    if (config.prothem && config.prothem.detail) {
      var detailconfig = config.prothem.detail.proconfig;  
      var customStyle = config.prothem.detail.customStyle;
      var popconfig = config.prothem.popup.proconfig;
      var popCustomStyle = config.prothem.popup.customStyle;
      update['detailconfig'] = detailconfig
      update['customStyle'] = customStyle
      update['popconfig'] = popconfig
      update['popCustomStyle'] = popCustomStyle
    }
    console.log("ddddd")
    console.log(config.integral)

    
    this.setData(update)
    this.getclass();
    this.getpagelist(that,1)
  },
  afterlogin: function (options, config, userinfo) {
    var that = this;
    this.setData({
      member: userinfo
    })
   
  },
  // 登录注册
  login(e) {
    var that = this;
    app.xc_sys_config.authop.oppower("login").then(function (res) {
      that.setData({
        "member": app.globalData.member
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.member){
      this.setData({
        member:app.globalData.member
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that=this;
    this.getclass();
    this.getpagelist(that,1)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that=this;
    that.getpagelist(that, -1) 
  },

})